package com.shujia.redis;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

public class RedisApi {
    public static void main(String[] args) {

        /**
         * 1、创建redis连接
         *
         */

        Jedis jedis = new Jedis("node3", 6379);


        //执行操作
        jedis.set("001", "shujia");

        String s = jedis.get("001");
        System.out.println(s);

        //关闭连接
        jedis.close();

    }

    Jedis jedis;
    JedisPool jedisPool;

    @Before
    public void init() {

        //创建连接
        jedis = new Jedis("node3", 6379);


        //连接池配置文件
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

        //最大连接数量
        jedisPoolConfig.setMaxTotal(100);
        //最大空闲连接数量
        jedisPoolConfig.setMaxIdle(10);

        //连接池    多个线程可以共享连接
        jedisPool = new JedisPool(jedisPoolConfig, "node3", 6379);

    }

    @Test
    public void pool() {
        //从连接池中获取连接
        Jedis resource = jedisPool.getResource();

        System.out.println(resource.get("001"));

        //使用完了将连接放回
        resource.close();
    }

    @After
    public void close() {
        jedis.close();
        jedisPool.close();
    }


    @Test
    public void string() {

        jedis.set("001", "shujia");
        System.out.println(jedis.get("001"));

        jedis.setex("002", 10, "java");

        System.out.println(jedis.ttl("002"));


    }

    @Test
    public void bit() {

        jedis.setbit("bit1", 10, true);

        System.out.println(jedis.getbit("bit1", 10));

        System.out.println(jedis.bitcount("bit1"));

    }


    @Test
    public void list() {

        jedis.lpush("l1", "java", "spark", "python");

        List<String> l1 = jedis.lrange("l1", 0, -1);

        System.out.println(l1);

        System.out.println(jedis.lpop("l1"));


        JedisPubSub jedisPubSub = new JedisPubSub() {
            @Override
            public void onMessage(String channel, String message) {

                System.out.println(channel);
                System.out.println(message);

            }
        };
        jedis.subscribe(jedisPubSub, "aaa");

    }

    @Test
    public void hashMap() throws Exception {

        String tableName = "student";

        //将所学生的信息存到redis中
        BufferedReader bufferedReader = new BufferedReader(new FileReader("data/students.txt"));

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            String[] split = line.split(",");
            String id = split[0];
            String name = split[1];
            String age = split[2];
            String sex = split[3];
            String clazz = split[4];

            String key = tableName + ":" + id;


            HashMap<String, String> map = new HashMap<>();
            map.put("name", name);
            map.put("age", age);
            map.put("sex", sex);
            map.put("clazz", clazz);


            //将数据保存到redis
            jedis.hmset(key, map);

        }


        Set<String> keys = jedis.keys("student*");
        System.out.println(keys);

    }


    @Test
    public void set() {
        jedis.sadd("s1", "java", "shujia", "hadoop", "spark", "spark");

        Set<String> s1 = jedis.smembers("s1");
        System.out.println(s1);

    }


}
